package com.goodlearning.gloriamobile.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.goodlearning.gloriamobile.R;
import com.goodlearning.gloriamobile.adapter.CustomAdapter;
import com.goodlearning.gloriamobile.adapter.CustomAdapter.IListItemController;
import com.goodlearning.gloriamobile.commons.Utils;
import com.goodlearning.gloriamobile.dto.GetAttendEndListDto;
import com.goodlearning.gloriamobile.net.JsonRequestThread.INetSuccessCallback;
import com.goodlearning.gloriamobile.net.JsonServiceRequester;
import com.goodlearning.gloriamobile.net.JsonWrapper;

/**
 * 종료된 강의 출결확인
 * @author rapty54
 *
 */
public class AdministrationInTheEnd extends BaseActivity {
	
	private JSONArray jsonArray;
	
	private GetAttendEndListDto attendEndListDto;
	 
	private ArrayList<GetAttendEndListDto> attendEndListDtos;
	
	private CustomAdapter<GetAttendEndListDto> customAdapter;
	
	ListView inTheEndClassListView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.administration_in_the_end_body_layout);
		init();
	}
	
	public void init(){
		baseTitleLeftBtn.setVisibility(View.VISIBLE);
		baseTitleLeftBtn.setTextColor(getResources().getColor(R.color.administration_fifth_text_change));
		baseTitleLeftBtn.setText(R.string.btn_char1_shift_1);
		baseTitleLeftBtn.setTextSize(20.0f);
		baseTitleTv.setText("종료된 강의 출결확인");
		baseTitleRightBtn.setVisibility(View.INVISIBLE);
		inTheEndClassListView=(ListView)findViewById(R.id.administratio_end_of_class_result_ListView);
		requestInTheEnd();
	}
	
	public void requestInTheEnd(){
		JsonServiceRequester.requestGetAttendList(AdministrationInTheEnd.this, new String(Base64.encodeBase64(getUserId().getBytes())), "1", new INetSuccessCallback() {
			
			@Override
			public void sendCallback(JsonWrapper jsonWrapper) {
				jsonArray=jsonWrapper.getRootArray();
				attendEndListDtos =new ArrayList<GetAttendEndListDto>();
				
				for(int i=0;i<jsonArray.size();i++){
					JSONObject jsonObject = (JSONObject) jsonArray.get(i);
					
					attendEndListDto=new GetAttendEndListDto();
					
					attendEndListDto.setDamdangGyosooMid(Utils.convertObjToString(jsonObject.get("damdangGyosooMid")));
					attendEndListDto.setLbNm(Utils.convertObjToString(jsonObject.get("LbNm")));
					attendEndListDto.sethYear(Utils.convertObjToString(jsonObject.get("hYear")));
					attendEndListDto.setLbIdx(Utils.convertObjToString(jsonObject.get("LbIdx")));
					attendEndListDto.settNm(Utils.convertObjToString(jsonObject.get("tNm")));
					attendEndListDto.setLecNm(Utils.convertObjToString(jsonObject.get("lecNm")));
					attendEndListDto.setHakgiNm(Utils.convertObjToString(jsonObject.get("hakgiNm")));
					attendEndListDto.setHbNm(Utils.convertObjToString(jsonObject.get("HbNm")));
					attendEndListDto.setMidSurvey(Utils.convertObjToString(jsonObject.get("midSurvey")));
					attendEndListDto.setEgNm(Utils.convertObjToString(jsonObject.get("egNm")));
					attendEndListDto.setsIdx(Utils.convertObjToString(jsonObject.get("sIdx")));
					attendEndListDto.setLIdx(Utils.convertObjToString(jsonObject.get("LIdx")));
					attendEndListDto.setFinalSurvey(Utils.convertObjToString(jsonObject.get("finalSurvey")));
					attendEndListDto.setJongGangDay(Utils.convertObjToString(jsonObject.get("jongGangDay")));
					attendEndListDto.setGaeGangDay(Utils.convertObjToString(jsonObject.get("gaeGangDay")));
					attendEndListDto.setNm(Utils.convertObjToString(jsonObject.get("nm")));
					attendEndListDto.setEgCd(Utils.convertObjToString(jsonObject.get("egCd")));
					
					attendEndListDtos.add(attendEndListDto);
				}
				
				setVisibleEmptyArea(attendEndListDtos.size()==0);
				
				customAdapter=new CustomAdapter<GetAttendEndListDto>(AdministrationInTheEnd.this, R.layout.administration_in_the_end_row, attendEndListDtos, listItemController);
				inTheEndClassListView.setAdapter(customAdapter);
				
				inTheEndClassListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
						Utils.log("=======================Item_Click================");
							GetAttendEndListDto getAttendEndListDto =new GetAttendEndListDto();
							
							getAttendEndListDto.setDamdangGyosooMid(((GetAttendEndListDto)parent.getItemAtPosition(position)).getDamdangGyosooMid());
							getAttendEndListDto.setLbNm(((GetAttendEndListDto)parent.getItemAtPosition(position)).getLbNm());
							getAttendEndListDto.sethYear(((GetAttendEndListDto)parent.getItemAtPosition(position)).gethYear());
							getAttendEndListDto.setLbIdx(((GetAttendEndListDto)parent.getItemAtPosition(position)).getLbIdx());
							getAttendEndListDto.settNm(((GetAttendEndListDto)parent.getItemAtPosition(position)).gettNm());
							getAttendEndListDto.setLecNm(((GetAttendEndListDto)parent.getItemAtPosition(position)).getLecNm());
							getAttendEndListDto.setHakgiNm(((GetAttendEndListDto)parent.getItemAtPosition(position)).getHakgiNm());
							getAttendEndListDto.setHbNm(((GetAttendEndListDto)parent.getItemAtPosition(position)).getHbNm());
							getAttendEndListDto.setMidSurvey(((GetAttendEndListDto)parent.getItemAtPosition(position)).getMidSurvey());
							getAttendEndListDto.setEgNm(((GetAttendEndListDto)parent.getItemAtPosition(position)).getEgNm());
							getAttendEndListDto.setsIdx(((GetAttendEndListDto)parent.getItemAtPosition(position)).getsIdx());
							getAttendEndListDto.setLIdx(((GetAttendEndListDto)parent.getItemAtPosition(position)).getLIdx());
							getAttendEndListDto.setFinalSurvey(((GetAttendEndListDto)parent.getItemAtPosition(position)).getFinalSurvey());
							getAttendEndListDto.setJongGangDay(((GetAttendEndListDto)parent.getItemAtPosition(position)).getJongGangDay());
							getAttendEndListDto.setGaeGangDay(((GetAttendEndListDto)parent.getItemAtPosition(position)).getGaeGangDay());
							getAttendEndListDto.setNm(((GetAttendEndListDto)parent.getItemAtPosition(position)).getNm());
							getAttendEndListDto.setEgCd(((GetAttendEndListDto)parent.getItemAtPosition(position)).getEgCd());
							
							Utils.changeActivityNSendData(AdministrationInTheEnd.this, AdministrationAgencyPractice.class, Intent.FLAG_ACTIVITY_CLEAR_TOP,"getAttendEndListDto", (GetAttendEndListDto)getAttendEndListDto);
						}
					});
			}
		});
	}
	
	IListItemController<GetAttendEndListDto> listItemController=new IListItemController<GetAttendEndListDto>() {

		@Override
		public View controlView(List<GetAttendEndListDto> items, int position,View convertView, ViewGroup parent) {
			
			GetAttendEndListDto attendListDto=(GetAttendEndListDto)items.get(position);
			
			((TextView)convertView.findViewById(R.id.administration_date_of_end_class_TextView)).setText(attendListDto.getHakgiNm());
			((TextView)convertView.findViewById(R.id.administration_end_class_proffessor_TextView)).setText(attendListDto.getNm());
			((TextView)convertView.findViewById(R.id.administration_end_class_of_state_TextView)).setText(attendListDto.getLecNm());
			
			return convertView;
		}
	};
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
}
